﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EasyFlow
{
    /// <summary>
    /// FlowControl.xaml 的交互逻辑
    /// </summary>
    public partial class FlowControl : UserControl
    {
        public ObservableCollection<StepViewModel> StepList
        {
            get { return (ObservableCollection<StepViewModel>)GetValue(StepListProperty); }
            set { SetValue(StepListProperty, value); }
        }
        public static readonly DependencyProperty StepListProperty =
            DependencyProperty.Register("StepList", typeof(ObservableCollection<StepViewModel>), typeof(FlowControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public ICommand StepDropCommand
        {
            get { return (ICommand)GetValue(StepDropCommandProperty); }
            set { SetValue(StepDropCommandProperty, value); }
        }

        public static readonly DependencyProperty StepDropCommandProperty =
            DependencyProperty.Register("StepDropCommand", typeof(ICommand), typeof(FlowControl), new PropertyMetadata(null));

        public ICommand StepDragOverCommand
        {
            get { return (ICommand)GetValue(StepDragOverCommandProperty); }
            set { SetValue(StepDragOverCommandProperty, value); }
        }
        public static readonly DependencyProperty StepDragOverCommandProperty =
            DependencyProperty.Register("StepDragOverCommand", typeof(ICommand), typeof(FlowControl), new PropertyMetadata(null));
        public ICommand StepDeleteCommand
        {
            get { return (ICommand)GetValue(StepDeleteCommandProperty); }
            set { SetValue(StepDeleteCommandProperty, value); }
        }
        public static readonly DependencyProperty StepDeleteCommandProperty =
            DependencyProperty.Register("StepDeleteCommand", typeof(ICommand), typeof(FlowControl), new PropertyMetadata(null));

        public StepViewModel CurrentSelectedStepViewModel
        {
            get { return (StepViewModel)GetValue(CurrentSelectedStepViewModelProperty); }
            set { SetValue(CurrentSelectedStepViewModelProperty, value); }
        }
        public static readonly DependencyProperty CurrentSelectedStepViewModelProperty =
            DependencyProperty.Register("CurrentSelectedStepViewModel", typeof(StepViewModel), typeof(FlowControl), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

        public FlowControl()
        {
            InitializeComponent();
        }

        private void TreeViewItem_Drop(object sender, DragEventArgs e)
        {
            StepDropCommand?.Execute(e.Data.GetData(DataFormats.Serializable));
            e.Handled = true;
        }

        private void TreeViewItem_DragOver(object sender, DragEventArgs e)
        {
            StepDragOverCommand?.Execute((sender as TreeViewItem).DataContext);
            e.Handled = true;
        }
        private void TreeView_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem clickedItem = GetClickedTreeViewItem(e);
            if (clickedItem != null)
            {
                CurrentSelectedStepViewModel = clickedItem.DataContext as StepViewModel;
                ContextMenu contextMenu = new ContextMenu();
                MenuItem editItem = new MenuItem { Header = "Edit" };
                editItem.Click += EditItem_Click;
                contextMenu.Items.Add(editItem);

                MenuItem deleteItem = new MenuItem { Header = "Delete" };
                deleteItem.Click += DeleteItem_Click;
                contextMenu.Items.Add(deleteItem);

                clickedItem.ContextMenu = contextMenu;
                clickedItem.ContextMenu.IsOpen = true;
            }
        }

        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            StepDeleteCommand?.Execute(null);
        }

        private void EditItem_Click(object sender, RoutedEventArgs e)
        {
            //ParameterSettingView parameterSettingView = new ParameterSettingView();
            //parameterSettingView.DataContext = selectedStepViewModel;
            //parameterSettingView.Show();
        }

        private TreeViewItem GetClickedTreeViewItem(MouseButtonEventArgs e)
        {
            DependencyObject obj = e.OriginalSource as DependencyObject;
            while(obj != null && !(obj is TreeViewItem))
            {
                obj = VisualTreeHelper.GetParent(obj);
            }
            return obj as TreeViewItem;
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            StepViewModel? viewModel = (sender as TreeView)!.SelectedItem as StepViewModel;
            if(viewModel != null)
                CurrentSelectedStepViewModel = viewModel;
        }
    }
}
